﻿using EazyCMS.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EazyCMS.Web.Plugin.Naire.admin
{
    public partial class naire_edit : EazyCMS.Web.UI.ManagePage
    {
        private string action = EazyEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = EazyRequest.GetQueryString("action");

            if (!string.IsNullOrEmpty(_action) && _action == EazyEnums.ActionEnum.Edit.ToString())
            {
                this.action = EazyEnums.ActionEnum.Edit.ToString();//修改类型
                this.id = EazyRequest.GetQueryInt("id");
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    return;
                }
                if (!new BLL.naire().Exists(this.id))
                {
                    JscriptMsg("信息不存在或已被删除！", "back");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                ChkAdminLevel("plugin_vote", EazyEnums.ActionEnum.View.ToString()); //检查权限
                TreeBind(); //绑定站点
                if (action == EazyEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
            }
        }

        #region 绑定站点=================================
        private void TreeBind()
        {
           
        }

        #endregion

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.naire bll = new BLL.naire();
            Model.naire model = bll.GetModel(_id);

          
            txtSortId.Text = model.sort_id.ToString();
          
            txtTitle.Text = model.title;
         
            txtStartTime.Text = model.start_time.ToString("yyyy-MM-dd");
            txtEndTime.Text = model.end_time.ToString("yyyy-MM-dd");
            txtContent.Value = model.content;
            //绑定投票选项
           
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            bool result = false;
            Model.naire model = new Model.naire();
            BLL.naire bll = new BLL.naire();

         
            model.sort_id = Utils.StrToInt(txtSortId.Text.Trim(), 99);
            model.title = txtTitle.Text.Trim();
         
            model.start_time = DateTime.Parse(this.txtStartTime.Text.Trim());
            model.end_time = DateTime.Parse(this.txtEndTime.Text.Trim());
            model.content = txtContent.Value;



            if (bll.Add(model) > 0)
            {
                AddAdminLog(EazyEnums.ActionEnum.Add.ToString(), "添加投票主题：" + model.title); //记录日志
                result = true;
            }
            return result;
           
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            bool result = false;
            BLL.naire bll = new BLL.naire();
            Model.naire model = bll.GetModel(_id);

          
            model.sort_id = Utils.StrToInt(txtSortId.Text.Trim(), 99);
            model.title = txtTitle.Text.Trim();
          
            model.start_time = DateTime.Parse(this.txtStartTime.Text.Trim());
            model.end_time = DateTime.Parse(this.txtEndTime.Text.Trim());
            model.content = txtContent.Value;



            if (bll.Update(model))
            {
                AddAdminLog(EazyEnums.ActionEnum.Edit.ToString(), "修改投票主题：" + model.title); //记录日志
                result = true;
            }

            return result;
        }
        #endregion

        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == EazyEnums.ActionEnum.Edit.ToString()) //修改
            {
                ChkAdminLevel("plugin_naire", EazyEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误！", string.Empty);
                    return;
                }
                JscriptMsg("修改在线投票成功！", "default.aspx");
            }
            else //添加
            {
                ChkAdminLevel("plugin_naire", EazyEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", string.Empty);
                    return;
                }
                JscriptMsg("添加在线投票成功！", "default.aspx");
            }
        }
    }
}